<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['branch'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['branch'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  $branchIdDetailArray = array();
  $branchName          = "";
  $userId              = "";
  $i                   = 0;  
  $branchId            = isset($_REQUEST['branchId']) ? $_REQUEST['branchId'] : 0;

  // after post : start 
  if(isset($_POST['branchName']))
  {
    if($branchId > 0)
    {
      $updateBranch = "UPDATE branch SET branchName  = '".$_POST['branchName']."'
                                   WHERE branchId    = '".$_REQUEST['branchId']."'";
      $updateBranchRes  = mysql_Query($updateBranch);
      if(!$updateBranchRes)
        die("Insert Query Not Inserted : ".mysql_error());
      else
        header("Location:branch.php");
    }
    else
    {
      $userId = $_SESSION['s_activeUserId'.$cfgPack];
      $insertQueary= "INSERT INTO branch (userId,branchName)
                            VALUE('".$userId."','".$_POST['branchName']."')";
      $branhResult = mysql_query($insertQueary);
      if(!$branhResult)
        die("Insert Query Not Inserted : ".mysql_error());
      else
        header("Location:branch.php");
    }
  }    
  // after post : end

  // delete branch : starts
  if(isset($_REQUEST['deleteId']) && $_REQUEST['deleteId'] > 0 )
  {
    $deleteBranch = "DELETE FROM branch WHERE branchId = ".$_REQUEST['deleteId'];
    $deleteRes = mysql_query($deleteBranch);
  }
  // delete branch : ends
  
  $selectBranch = "SELECT branchId,branchName
                     FROM branch
                 ORDER BY branchName";
  $selectBranchResult = mysql_query($selectBranch);
  while($itemInRow = mysql_fetch_array($selectBranchResult))
  {                                                    
    $branchIdDetailArray[$i]['branchId']   = $itemInRow['branchId'];
    $branchIdDetailArray[$i]['branchName'] = $itemInRow['branchName'];
    
    if($itemInRow['branchId'] == $branchId)
    {
    	$branchName  = $itemInRow['branchName']; 
    }
    $i++;     
  }    
   
  include("./bottom.php");
  $smarty->assign("branchId",$branchId);
  $smarty->assign("userId",$userId);
  $smarty->assign("branchName",$branchName);
  $smarty->assign("branchIdDetailArray",$branchIdDetailArray);
  $smarty->assign("i",$i);
  $smarty->display("branch.tpl");
}
?>